//
//  SQCLLocationManager.m
//  QT
//
//  Created by 甘世清 on 2025/1/4.
//

#import "SQCLLocationManager.h"
#import <JZLocationConverter/JZLocationConverter.h>
@interface SQCLLocationManager()<CLLocationManagerDelegate>

@property (nonatomic , strong) citySuccessBlock  block;

@end


static SQCLLocationManager * instance = nil;
static dispatch_once_t onceToken;

@implementation SQCLLocationManager

+(instancetype)sharedManager{
 
    dispatch_once(&onceToken, ^{
        instance = [[SQCLLocationManager alloc]init];
     
    });
    return instance;
}
-(instancetype)init{
    
    self = [super init];
    if (self) {
        self.delegate = self;
        self.desiredAccuracy = kCLLocationAccuracyBest;
        self.distanceFilter = 1;
       
    }
    return self;
}
 
 
-(void)SQ_startUpdatingLocation{
    /** 申请授权, AlwaysAuthorization:永久授权 */
    [[SQCLLocationManager sharedManager] requestWhenInUseAuthorization];
    if ([SQCLLocationManager sharedManager].authorizationStatus != kCLAuthorizationStatusDenied ){
        [[SQCLLocationManager sharedManager] startUpdatingLocation];
    }else{
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"定位权限未打开,请前往设置开启权限进行定位")];
    }
}
 
-(void)SQ_Location:(citySuccessBlock)city{
    self.block = city;
    [[SQCLLocationManager sharedManager] SQ_startUpdatingLocation];
}
 
 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations{
    if (locations.count<=0) {
        return;
    }
    if (self.times >0) {
        NSString * new = [NSString SQ_currentTime];
        if ([new intValue] - self.times < 10) {
            return;
        }
    }
    [[SQCLLocationManager sharedManager] stopUpdatingLocation];
    self.times = [[NSString SQ_currentTime] intValue];
    CLLocation * location = locations.firstObject;
    CLLocationCoordinate2D loca = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
    CLLocationCoordinate2D loca2 = [JZLocationConverter wgs84ToGcj02:loca];
    UserModel * model = [UserModel share];
    model.lat = loca2.latitude;
    model.lng = loca2.longitude;
    [UserModel updateLocalUserModel:model];
    CLGeocoder *gecoder = [[CLGeocoder alloc]init];
    WS(weakSelf);
    [gecoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0) {
            CLPlacemark * place = placemarks.firstObject;
            NSString *city = place.locality;
            if ([city isNotEmpty]) {
                [weakSelf SQ_upinfo:loca2.latitude lng:loca2.longitude city:city];
            }else if([place.administrativeArea isNotEmpty]) {
                [weakSelf SQ_upinfo:loca2.latitude lng:loca2.longitude city:place.administrativeArea];
            }else{
                [weakSelf SQ_upinfo:loca2.latitude lng:loca2.longitude city:@""];
            }
        }
    }];
}
-(void)SQ_upinfo:(float)lat lng:(float)lng city:(NSString*)city{
   
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%.7f",lat] forKey:@"lat"];
    [dic setObject:[NSString stringWithFormat:@"%.7f",lng] forKey:@"lng"];
    [dic setObject:city forKey:@"city"];
    if (self.block) {
        self.block(dic);
    }
    [HttpTool post:@"user/config" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
      
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{
    DTLog(@"报错了%@",error);
    [YJProgressHUD hide];
}
 
 
- (void)dealloc{
    instance = nil;
   
}
@end
